# -*- coding: utf-8 -*-
'''
@author: Hugo
@file: ypudao.py
@time: 2021/8/19 11:13
'''
import random
import time
from hashlib import md5

import requests

class Crawl():
    def __init__(self):

        self.headers={
            'Cookie':'OUTFOX_SEARCH_USER_ID=693345967@120.229.93.204; OUTFOX_SEARCH_USER_ID_NCOO=1523975882.4042761; _ntes_nnid=945857e8fd7643b59499b77416662e12,1629163100008; JSESSIONID=aaa5pcwATATHihIhtWCTx; ___rl__test__cookies=1629352901753',
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36',
            'Referer':'https://fanyi.youdao.com/',   # 防盗链
        }
        self.salt = str(int(time.time())*1000) + str(random.randint(1,10))
        self.sign = "fanyideskweb" + '你好' + self.salt +"Y2FYu%TNSbMCxc3t2u^XT"
        print(self.salt)
        print(self.salt[:-1])

    def md(self,content):
        m = md5()
        m.update(content.encode(encoding = 'utf8'))
        str_md5 = m.hexdigest()
        print(str_md5)
        return str_md5

    def crawl(self):
        parmas = {
            'i':'你好',
            'from':'AUTO',
            'to':'AUTO',
            'smartresult':'dict',
            'client':'fanyideskweb',
            'salt':self.salt,
            'sign':self.md("fanyideskweb" + '你好' + self.salt +"Y2FYu%TNSbMCxc3t2u^XT"),
            'lts':self.salt[:-1],
            'bv':'24ecb70ba6203e4453baed50aa26b78e',
            'doctype':'json',
            'version':'2.1',
            'keyfrom':'fany.web',
            'action':'FY_BY_REALTlME',
        }

        url = 'https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
        res = requests.post(url = url,data = parmas,headers = self.headers)
        print(res.json())

if __name__ == '__main__':
    s = Crawl()
    s.crawl()